<?php
	class CaijiAction extends CommonAction{
		public function index(){
			
			$this->assign('pagename','所有采集规则');
			
			$cj=M('article_article');
			$count=$cj->count();
			
			import('ORG.Util.Page');
			
			$page=new Page($count,50);
			$pageshow=$page->show();
			$pageshow=str_replace('<a ','<a class="btn btn-primary" ',$pageshow);
			$this->assign('pageshow',$pageshow);
			$where['fullflag']=1;
			$cjArr=$cj->where($where)->limit($page->firstRow.','.$page->listRows)->order('articleid desc')->select();
			$this->assign('cjinfos',$cjArr);
			$this->display();
		
		}

		protected function getUrlContent($url) {
			
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL,$url);//抓取指定网页
        curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($curl,  CURLOPT_CUSTOMREQUEST, 'GET');//post提交方式
        curl_setopt($curl, CURLOPT_POSTFIELDS, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
		$output = curl_exec($curl);
		curl_close($curl);
		return $output;
		}	

			
		//多项采集或者全部采集
		public function listscaiji($actionlogid=''){
			echo '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>';
			if(empty($actionlogid)){
				$actionlogid=$_GET['id'];
			}
			$alog=M('action_log');
			$actionwhere['actionlogid']=$actionlogid;
			$caijiids=$alog->where($actionwhere)->field('actionlog')->select();
			$id=$caijiids[0]['actionlog'];
			$caijiid=$caijiids[0]['actionlog'];
			$idArr=trim($id,',');
			$idArrs=explode(",",$idArr);
			if(!isset($_GET['nowid'])){
			$_GET['nowid']=$idArrs[0];
			}
			
			$caijiid=$_GET['nowid'];
			if($caijiid<1000){
						$cDirId=0;
					}else{
					$cDirId=substr($caijiid, 0, -3);
					
					}
			if( isset($_GET['nowid']) and !isset($_GET['pageid']) ){	
			
				//取对方的章节及内容
				$caijiInfo['caijiurl']='https://www.81new.com/'.$cDirId.'/'.$_GET['nowid'].'/';
				$content=$this->getUrlContent($caijiInfo['caijiurl']);
				$content=mb_convert_encoding($content , "UTF-8", "GBK"); 
				$find=false;
				$articlenamereu='/<h1>(.*)<\/h1>/';
				preg_match_all($articlenamereu,$content,$articlename);
				$initial=getFirstCharter($articlename[1][0]);
				$articleauthorreu='/作者：<a href="(.*)">(.*)<\/a>/U';
				preg_match_all($articleauthorreu,$content,$author);
				$articleintroreu = '/<p class="jj">(.*)<\/p>/';
				preg_match_all($articleintroreu,$content,$intros);
				$intro = str_replace('<p>','',$intros[1][0]);
				$intro = str_replace('</p>','',$intro);
				
				$typereu='/<meta name="keywords" content="(.*)"\/>/Us';
				preg_match_all($typereu,$content,$type);
				$typelist=mb_strlen($type[1][0],'utf-8');
				
				$typename=mb_substr($type[1][0],-4, $typelist ,'utf-8');
				
				if($typename=='玄幻小说'){
					$tpeid=1;
				}elseif($typename=='仙侠小说'){
					$tpeid=2;
				}elseif($typename=='都市小说'){
					$tpeid=3;
				}elseif($typename=='历史小说'){
					$tpeid=4;
				}elseif($typename=='军史小说'){
					$tpeid=5;
				}elseif($typename=='恐怖小说'){
					$tpeid=6;
				}elseif($typename=='科幻小说'){
					$tpeid=7;
				}elseif($typename=='网游小说'){
					$tpeid=8;
				}else{
					$tpeid=12;
				}
				$articlestatusreu='/<span>状态：(.*)<\/span>/U';
				preg_match_all($articlestatusreu,$content,$status);
				if($status[1][0]=='连载'){
					$fullflag=1;
				}else{
					$fullflag=0;
				}
				$Article=M('article_article');
				$where['articlename']=$articlename[1][0];
				
				$returnid=$Article->where($where)->field('articleid')->select();
				$return=$returnid[0]['articleid'];
				if(empty($return)){//判断该小说是否为第一采集
				$slq['articlename']=$articlename[1][0];
				$slq['author']=$author[2][0];
				$slq['intro']=$intro;
				$slq['poster']='admin';
				$slq['caijiid']=$caijiid;
				$slq['sortid']=$tpeid;
				$slq['fullflag']=$fullflag;
				$slq['imgflag']=1;
				$slq['initial']=$initial;
				$slq['postdate']=time();
				$slq['lastupdate']=time();
				$slq['poster']='admin';
				$return=$Article->add($slq);
				}else{//否则更新该小说的信息
				$where_article['articleid']=$return;
				$article_data = array('author'=>$author[2][0],'intro'=>$intro,'sortid'=>$tpeid,'fullflag'=>$fullflag,'lastupdate'=>time(),'initial'=>$initial,'poster'=>'admin','imgflag'=>1,'postdate'=>time());
				$Article->where($where_article)->save($article_data);
				}
				$Dir=D('Dir');//查询写入路径
				$where_dir['id']=1;
				$dirs=$Dir->where($where_dir)->select();
				
				if($return<1000){
						$dirId=0;
					}else{
					$dirId=substr($return, 0, -3);
					
					}
				$chatper=$dirs[0]['txt_dir'].$dirId.'/'.$return;
			
				if(!file_exists($path)){
					mkdir($chatper,0777,true);//这个是在盘根目录生成文件
				}
				//采集封面图片
				$img='https://www.81new.com/files/article/image/'.$cDirId.'/'.$caijiid.'/'.$caijiid.'s.jpg';
			
				$fileurl=$dirs[0]['img_dir'].$dirId.'/'.$return;
				if(!file_exists($path)){
					mkdir($fileurl,0777,true);//这个是在c盘根目录生成文件
				}
				$image = file_get_contents($img); 
				file_put_contents($fileurl.'/'.$return.'s.jpg', $image); //保存图片到本地地址
				$where_lastchapter['caijiid']=$caijiid;
				$lastchapter=$Article->where($where_lastchapter)->getField('lastchapter');//获取本网站该小说的最新章节
				$reu='/<div class="ml_list">(.*)<\/div>/Us';//连载中
				preg_match_all($reu,$content,$chatperlist);
				$chapterReu='/<li><a href="(.*).html">(.*)<\/a><\/li>/Us';				
				if(preg_match_all($chapterReu,$chatperlist[0][0],$conArr)){
					$i=0;
					foreach($conArr[2] as $conInfo){//遍历循环对比小说的最新章节
						if($find){
							$nextName=$conInfo;
							break;
						}
						if($conInfo == $lastchapter){
							$find=true;
						}
						if(strstr($lastchapter,$conInfo)){
							$find=true;
						}
						$i=$i+1;
					}
					if($find == false){
						$i=0;
					}
					
					if($conArr[2][$i]){	
						$articleid =$conArr[1][$i]; 
							
						$this->success('分析成功对方章节，即将采集！',U('Caiji/listscaiji/')."/id/".$actionlogid."/nowid/".$_GET['nowid']."/pageid/".$conArr[1][$i]."/return/".$return , 0);
						//跳转采集
					}else{
						$last=end($idArrs);
						
						foreach($idArrs as $ids){
							if($_GET['nowid'] == $ids and $_GET['nowid'] !=$last ){//获取下一本要采集小说的ID
								$nextnovelid=current($idArrs);
								var_dump($nextnovelid);
								$nextUrl=U('Caiji/listscaiji/')."/id/".$actionlogid."/nowid/".$nextnovelid;
								
								$this->success("本小说采集已经完成！马上跳到下一本小说继续采集！",$nextUrl,0);
								break;
							}else if($_GET['nowid'] == $last){
								echo "<h1>所有小说采集已经完成！！</h1><a href='".U('Caiji/index')."'><h3>点此返回</h3></a>";
								
								exit();
							}
						}	
					}
				}else{
					echo "<h1>出现错误，传参错误！匹配失败！</h1>";
				}
			}else if( isset($_GET['pageid'])){
				$nowId=$_GET['nowid'];
				$returnId=$_GET['return'];
				$oneurl='https://www.81new.com/'.$cDirId.'/'.$nowId.'/';
				$caijiurl=$oneurl.$_GET['pageid'].'.html';//组装远程采集的对象路径
				$content=$this->getUrlContent($caijiurl);
				$content=mb_convert_encoding($content , 'UTF-8', 'GBK');
				$reutitle='/<h3>(.*)<\/h3>/';
				if(preg_match($reutitle,$content,$titles)){
					$title=trim($titles[1]);
					
					
				}else{
					echo "<h1>标题匹配不成功!</h1>";exit();
				}
				$reucontent='/<p class="articlecontent" id="articlecontent">(.*)<\/p>/Us';
				
				if(preg_match($reucontent,$content,$con)){
					$novelcon=trim($con[1]);
				}else{
					echo "<h1>内容匹配不成功!</h1>";exit();
				}		
				//入库
				$C=M('article_chapter');
				$jieqi=M('article_article');
				$where_articleinfo['articleid']=$returnId;
				$articleinfo=$jieqi->where($where_articlename)->select();
				$where='`articleid`='.$returnId.' and `chaptername` LIKE "'.$title.'"';
				$haveThis=$C->where($where)->find();
				$replaceArray = array('[八一中文网 请记住' => '', 'www.81new.com' => '' ,'手机版访问 m.81new.com 绿色无弹窗]' => '' ,'<br />' => '' );//替换采集源的广告
				$novelcon = strtr($novelcon , $replaceArray);
				$novelcon = iconv('UTF-8','gbk',$novelcon);
				$strs=mb_strlen($novelcon, 'UTF-8') . '';
				$total_number= $strs/2;
				
				if(!is_array($haveThis)){
					$addInfo['articleid']=$returnId;
					$addInfo['articlename']=$articleinfo[0]["articlename"];
					$addInfo['chaptername']=$title;
					$addInfo['postdate']=time();
					$addInfo['lastupdate']=time();
					$addInfo['size']=$total_number;
					$insert=$C->add($addInfo);
					if($insert==false){
						echo "<h1>".$addInfo['articlename']."本条记录插入失败</h1>";exit();
					}
			
					$Dir=D('Dir');//查询写入路径
					$where_dir['id']=1;
					$dirs=$Dir->where($where_dir)->select();
					if($returnId<1000){
						$dirId=0;
					}else{
					$dirId=substr($returnId, 0, -3);
					
					}
					$fileurl=$dirs[0]['txt_dir'].$dirId.'/'.$returnId.'/'.$insert.'.txt';
					
				$myfile = fopen($fileurl, "a") or die("Unable to open file!");//生成index.opf文件
				fwrite($myfile, $novelcon);
					$fileid=$_GET['pageid'];
					$jieqi=M('article_article');
					$lastchaptercondision['caijiid']=$nowId;
					$where_chapters_number['articleid']=$returnId;
					$chapters_number=$C->where($where_chapters_number)->count();
					$total_numbers=$jieqi->where($lastchaptercondision)->field('size')->select();
					$all_total_number=$total_numbers[0]['size']+$total_number;
					$last_chapter_data = array('lastchapterid'=>$fileid,'lastchapter'=>$title,'chapters'=>$chapters_number,'size'=>$all_total_number);
					$jieqilastchapter=$jieqi->where($lastchaptercondision)->setField($last_chapter_data);
				}
				
				//匹配下一页
				
				$nextpage='/<a href="(.*).html">下一章<\/a>/U';
				if(preg_match($nextpage,$content,$nexts)){
					$nextArrs=explode('/',$nexts[1]);
					$nextPageId=$nextArrs[3];
					if($nextPageId==0){
						//生成目录模板xml
						$where_chapters_number['articleid']=$returnId;
						$xiaoshuodetail=$C->where($where_chapters_number)->select();
						$file = '<?xml version="1.0" encoding="ISO-8859-1"?>
							<package id="'.$_GET['return'].'">
								<articleinfo>
								<articleid>'.$_GET['return'].'</articleid>
								<postdate>'.$articleinfo[0]["postdate"].'</postdate>
								<lastupdate>'.$articleinfo[0]["lastupdate"].'</lastupdate>
								<articlename>'.$articleinfo[0]["articlename"].'</articlename>
								<author>'.$articleinfo[0]["author"].'</author>
								<keywords></keywords>
								<intro>'.$articleinfo[0]["intro"].'</intro>
								<sortid>'.$articleinfo[0]["sortid"].'</sortid>
								<typeid>'.$articleinfo[0]["typeid"].'</typeid>
								<size>'.$articleinfo[0]["size"].'</size>
								<firstflag>'.$articleinfo[0]["firstflag"].'</firstflag>
								<fullflag>'.$articleinfo[0]["fullflag"].'</fullflag>
								<imgflag>'.$articleinfo[0]["imgflag"].'</imgflag>
								<notice>声明：本软件只提供服务测试和学习使用，如有用于非法用途而造成个人或国家财产损失的，请立即到相关部门自首！</notice>
								</articleinfo>';	
								$filecenter = '';
								foreach ($xiaoshuodetail as $vo) {
								$filecenter .= '<item chapterid="'.$vo["chapterid"].'" siteid="0" articleid="'.$_GET['return'].'" articlename="'.$articlename[0]["articlename"].'" volumeid="0" posterid="1" poster="admin" postdate="'.$articlename[0]["postdate"].'" lastupdate="'.$articlename[0]["lastupdate"].'" chaptername="'.$vo["chaptername"].'" chapterorder="1" size="'.$vo["size"].'" saleprice="0" salenum="0" totalcost="0" attachment="a:0:{}" summary=" " isimage="0" isvip="0" chaptertype="0" power="0" display="0" />';
								}
							$file .='<chapters>'
								.$filecenter.
								'</chapters>';
							writefile($dirs[0]['txt_dir'].$dirId.'/'.$returnId.'/index.opf',$file);
						//匹配下一本书的ID
						foreach($idArrs as $ids){
							if($nowId == $ids and $nowId !=$last ){//取下一个ID
								$nextnovelid=current($idArrs);
								$nextUrl=U('Caiji/listscaiji/').'/id/'.$actionlogid.'/nowid/'.$nextnovelid;
								$this->success("本小说采集已经完成！马上跳到下一本小说继续采集！",$nextUrl,0);
								break;
							}else if($nowId == $last){
								echo "<h1>所有小说采集已经完成！！</h1><a href='".U('Caiji/index')."'><h3>点此返回</h3></a>";
								exit();
							}
							
						}
						exit();
					}
					
				}else{
					echo "<h1>下一页ID匹配不成功!</h1>";
					exit();
				}
				$nextUrl=U('Caiji/listscaiji/').'/id/'.$_GET['id'].'/pageid/'.$nextPageId.'/nowid/'.$caijiid.'/caijiid/'.$caijiid.'/return/'.$returnId;
				//跳转到下一条
				$this->success($title.' 采集成功，即将跳转到下一章',$nextUrl,0);
			
			}
		}

		
		//单本书采集
		public function caijias($caijiid=''){
			echo '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>';
			$caijiInfo['caijiurl']='https://www.81new.com/';
			if($caijiid<1000){
						$cDirId=0;
					}else{
					$cDirId=substr($caijiid, 0, -3);
					
					}
			if(!isset($_GET['pageid'])){
				//取对方的章节及内容
				$caijiInfo['caijiid']=$caijiid;
				$oneurl=$caijiInfo['caijiurl'].$cDirId.'/'.$caijiInfo['caijiid'].'/';
				$content=$this->getUrlContent($oneurl);
				$content=mb_convert_encoding($content , "UTF-8", "GBK"); 
				$find=false;
				$articlenamereu='/<h1>(.*)<\/h1>/';
				preg_match_all($articlenamereu,$content,$articlename);
				$initial=getFirstCharter($articlename[1][0]);
				$articleauthorreu='/作者：<a href="(.*)">(.*)<\/a>/U';
				preg_match_all($articleauthorreu,$content,$author);
				
				$articleintroreu = '/<p class="jj">(.*)<\/p>/';
				preg_match_all($articleintroreu,$content,$intros);
				$intro = str_replace('<p>','',$intros[1][0]);
				$intro = str_replace('</p>','',$intro);
				$typereu='/<meta name="keywords" content="(.*)"\/>/Us';
				preg_match_all($typereu,$content,$type);
				$typelist=mb_strlen($type[1][0],'utf-8');
				$typename=mb_substr($type[1][0],-4, $typelist ,'utf-8');
				if($typename=='玄幻小说'){
					$tpeid=1;
				}elseif($typename=='仙侠小说'){
					$tpeid=2;
				}elseif($typename=='都市小说'){
					$tpeid=3;
				}elseif($typename=='历史小说'){
					$tpeid=4;
				}elseif($typename=='军史小说'){
					$tpeid=5;
				}elseif($typename=='恐怖小说'){
					$tpeid=6;
				}elseif($typename=='科幻小说'){
					$tpeid=7;
				}elseif($typename=='网游小说'){
					$tpeid=8;
				}else{
					$tpeid=12;
				}
				
				
				$articlestatusreu='/<span>状态：(.*)<\/span>/U';
				preg_match_all($articlestatusreu,$content,$status);
				
				if($status[1][0]=='连载'){
					$fullflag=1;
				}else{
					$fullflag=0;
				}
				$Article=M('article_article');
				$where['articlename']=$articlename[1][0];
				$returnid=$Article->where($where)->field('articleid')->select();
				$return=$returnid[0]['articleid'];
				if(empty($return)){
				$slq['articlename']=$articlename[1][0];
				$slq['author']=$author[2][0];
				$slq['intro']=$intro;
				$slq['poster']='admin';
				$slq['caijiid']=$caijiid;
				$slq['sortid']=$tpeid;
				$slq['fullflag']=$fullflag;
				$slq['imgflag']=1;
				$slq['initial']=$initial;
				$slq['postdate']=time();
				$slq['lastupdate']=time();
				$slq['poster']='admin';
				$return=$Article->add($slq);
				}else{
				$where_article['articleid']=$return;
				$article_data = array('author'=>$author[2][0],'intro'=>$intro,'sortid'=>$tpeid,'fullflag'=>$fullflag,'lastupdate'=>time(),'initial'=>$initial,'poster'=>'admin','imgflag'=>1,'postdate'=>time(),'sortid'=>$tpeid);
				
				$Article->where($where_article)->save($article_data);
				}
				$Dir=D('Dir');//查询写入路径
				$where_dir['id']=1;
				$dirs=$Dir->where($where_dir)->select();
				
				if($return<1000){
						$dirId=0;
					}else{
					$dirId=substr($return, 0, -3);
					
					}
				$chatper=$dirs[0]['txt_dir'].$dirId.'/'.$return;
			
				if(!file_exists($path)){
					mkdir($chatper,0777,true);//这个是在根目录生成文件
				}
				//采集封面图片
				$img='https://www.81new.com/files/article/image/'.$cDirId.'/'.$caijiid.'/'.$caijiid.'s.jpg';
				
				$fileurl=$dirs[0]['img_dir'].$dirId.'/'.$return;
				if(!file_exists($path)){
					mkdir($fileurl,0777,true);//这个是在c盘根目录生成文件
				}
				$image = file_get_contents($img); 
				file_put_contents($fileurl.'/'.$return.'s.jpg', $image); //保存图片到本地地址
				

				$where_lastchapter['caijiid']=$caijiid;
				$lastchapter=$Article->where($where_lastchapter)->getField('lastchapter');
				$reu='/<div class="ml_list">(.*)<\/div>/Us';//连载中
				preg_match_all($reu,$content,$chatperlist);
				$chapterReu='/<li><a href="(.*).html">(.*)<\/a><\/li>/Us';			
				if(preg_match_all($chapterReu,$chatperlist[0][0],$conArr)){
					$i=0;
					foreach($conArr[2] as $conInfo){
						if($find){
							$nextName=$conInfo;
							break;
						}
						if($conInfo == $lastchapter){
							$find=true;
						}
						if(strstr($lastchapter,$conInfo)){
							$find=true;
						}
						$i=$i+1;
					}
					
					if($find == false){
						$i=0;
					}
					
					if($conArr[2][$i]){	
						$articleid =$conArr[1][$i]; 
					
						$this->success('分析成功对方章节，即将采集！',U('Caiji/caijias/')."/id/".$return."/pageid/".$conArr[1][$i]."/caijiid/".$caijiid."/articleid/".$articleid."/return/".$return , 0);
						//跳转采集
					}else{
						 echo "<h1>采集完毕！</h1>";
						
						exit();
					}
				}else{
					echo "<h1>出现错误，传参错误！匹配失败！</h1>";
				}
			}else{
				
				$oneurl=$caijiInfo['caijiurl'].$cDirId.'/'.$caijiid.'/';
				$caijiurl=$oneurl.$_GET['pageid'].".html";
				$content=$this->getUrlContent($caijiurl);
				$content=mb_convert_encoding($content , "UTF-8", "GBK");
				$reutitle='/<h3>(.*)<\/h3>/';
				if(preg_match($reutitle,$content,$titles)){
					$title=trim($titles[1]);
					
					
				}else{
					echo "<h1>标题匹配不成功!</h1>";exit();
				}
				$reucontent='/<p class="articlecontent" id="articlecontent">(.*)<\/p>/Us';
				
				if(preg_match($reucontent,$content,$con)){
					$novelcon=trim($con[1]);
				}else{
					echo "<h1>内容匹配不成功!</h1>";exit();
				}		
				//入库
				$C=M('article_chapter');
				$jieqi=M('article_article');
				$where_articleinfo['articleid']=$_GET['return'];
				$articleinfo=$jieqi->where($where_articlename)->select();
				$where='`articleid`='.$_GET['return'].' and `chaptername` LIKE "'.$title.'"';
				$haveThis=$C->where($where)->find();
				$fileid=$_GET['articleid'];
				$replaceArray = array('[八一中文网 请记住' => '', 'www.81new.com' => '' ,'手机版访问 m.81new.com 绿色无弹窗]' => '' ,'<br />' => '' );
				$novelcon = strtr($novelcon , $replaceArray);
				$novelcon = iconv('UTF-8','gbk',$novelcon);
				
				$strs=mb_strlen($novelcon, 'UTF-8') . '';
				$total_number= $strs/2;
				
				if(!is_array($haveThis)){
					$addInfo['articleid']=$_GET['return'];
					$addInfo['articlename']=$articleinfo[0]["articlename"];
					$addInfo['chaptername']=$title;
					$addInfo['postdate']=time();
					$addInfo['lastupdate']=time();
					$addInfo['size']=$total_number;
					$insert=$C->add($addInfo);
					if($insert==false){
						echo "<h1>".$addInfo['articlename']."本条记录插入失败</h1>";exit();
					}
			
					$Dir=D('Dir');//查询写入路径
					$where_dir['id']=1;
					$dirs=$Dir->where($where_dir)->select();
					if($_GET['return']<1000){
						$dirId=0;
					}else{
					$dirId=substr($_GET['return'], 0, -3);
					
					}
					$fileurl=$dirs[0]['txt_dir'].$dirId.'/'.$_GET['return'].'/'.$insert.'.txt';
					
				$myfile = fopen($fileurl, "a") or die("Unable to open file!");//这个是在c盘根目录生成文件
				fwrite($myfile, $novelcon);
					$jieqi=M('article_article');
					$fileid=$_GET['articleid'];
					$lastchaptercondision['caijiid']=$_GET['bookid'];
					$where_chapters_number['articleid']=$_GET['return'];
					$chapters_number=$C->where($where_chapters_number)->count();
					$total_numbers=$jieqi->where($lastchaptercondision)->field('size')->select();
					$all_total_number=$total_numbers[0]['size']+$total_number;
					$last_chapter_data = array('lastchapterid'=>$fileid,'lastchapter'=>$title,'chapters'=>$chapters_number,'size'=>$all_total_number);
					$jieqilastchapter=$jieqi->where($lastchaptercondision)->setField($last_chapter_data);
					 
					
				}	
				//匹配下一页
				$nextpage='/<a href="(.*).html">下一章<\/a>/U';
				if(preg_match($nextpage,$content,$nexts)){
					$nextArrs=explode("/",$nexts[1]);
					$nextid=$nextArrs[3];
					if($nextid==0){
						//生成目录模板xml
					$where_chapters_number['articleid']=$_GET['return'];
					
					 $xiaoshuodetail=$C->where($where_chapters_number)->select();
				
						$file = '<?xml version="1.0" encoding="ISO-8859-1"?>
									<package id="'.$_GET['return'].'">
									<articleinfo>
									<articleid>'.$_GET['return'].'</articleid>
									<postdate>'.$articleinfo[0]["postdate"].'</postdate>
									<lastupdate>'.$articleinfo[0]["lastupdate"].'</lastupdate>
									<articlename>'.$articleinfo[0]["articlename"].'</articlename>
									<author>'.$articleinfo[0]["author"].'</author>
									<keywords></keywords>
									<intro>'.$articleinfo[0]["intro"].'</intro>
									<sortid>'.$articleinfo[0]["sortid"].'</sortid>
									<typeid>'.$articleinfo[0]["typeid"].'</typeid>
									<size>'.$articleinfo[0]["size"].'</size>
									<firstflag>'.$articleinfo[0]["firstflag"].'</firstflag>
									<fullflag>'.$articleinfo[0]["fullflag"].'</fullflag>
									<imgflag>'.$articleinfo[0]["imgflag"].'</imgflag>
									<notice>声明：本软件只提供服务测试和学习使用，如有用于非法用途而造成个人或国家财产损失的，请立即到相关部门自首！</notice>
									</articleinfo>';	
						$filecenter = '';
						
						foreach ($xiaoshuodetail as $vo) {
						$filecenter .= '<item chapterid="'.$vo["chapterid"].'" siteid="0" articleid="'.$_GET['return'].'" articlename="'.$articlename[0]["articlename"].'" volumeid="0" posterid="1" poster="admin" postdate="'.$articlename[0]["postdate"].'" lastupdate="'.$articlename[0]["lastupdate"].'" chaptername="'.$vo["chaptername"].'" chapterorder="1" size="'.$vo["size"].'" saleprice="" salenum="" totalcost="" attachment="a:0:{}" summary=" " isimage="" isvip="" chaptertype="" power="" display="" />';
						
						
						}
						$file .='<chapters>'
								.$filecenter.
								'</chapters>';
								
								
						writefile($dirs[0]['txt_dir'].$dirId.'/'.$_GET['return']."/index.opf",$file);
						echo "<h1>采集已经完成！</h1>";
						
						exit();
					}
					
				}else{
					echo "<h1>下一章ID匹配不成功!</h1>";
					exit();
				}
				
				$nextUrl=U('Caiji/caijias/')."/id/".$_GET['id']."/pageid/".$nextid."/bookid/".$caijiid."/caijiid/".$caijiid."/return/".$_GET['return'];
				
				//还要更新时间
				$N=M('Novel');
				$novelin['update_time']=time();
				$novelin['id']=$caijiInfo['novelid'];
				$N->save($novelin);
				//跳转到下一条
				$this->success($title." 采集成功，即将跳转到下一条",$nextUrl,0);
			}
		}
		
		//采集单本小说
		public function caiji(){
	
			$this->caijias($_GET['id']);
			
		}
		
		//采集多本小说
		public function manycaiji(){
			//取对方的小说
			$orderid=$_GET['id'];
			
			$manyid = rtrim($orderid , ",");
				
				$alog=M('action_log');
				$data['actionlog']=$manyid;
				$actionlogid=$alog->add($data);
				
				$this->listscaiji($actionlogid);
	
			}
		
		//采集范围内小说
		public function rangecaiji(){
		
			$startID=$_POST['startID'];
			$endID=$_POST['endID'];
			
			//取对方的小说
			$lists=array();
			for($i=$startID; $i<$endID; $i++){
				//$b=$this->caijias($i);
				$lists[]=$i;
				}
			
				$manyid = implode(",", $lists);
				
				$alog=M('action_log');
				$data['actionlog']=$manyid;
				$actionlogid=$alog->add($data);
				
				$this->listscaiji($actionlogid);
	
			}	
		
		//采集网站所有小说
		public function allcaiji(){
			//取对方的小说
			$lists=array();
			for($i=1; $i<100000; $i++){
				//$b=$this->caijias($i);
				$lists[]=$i;
				}
			
				$manyid = implode(",", $lists);
					
				$alog=M('action_log');
				$data['actionlog']=$manyid;
				$actionlogid=$alog->add($data);
				$this->listscaiji($actionlogid);
	
			}
		
		public function add(){
			$this->assign('pagename','添加采集规则');
			$this->assign('checkcaiji',' active');
			$this->display();
		}
		public function addcaiji(){
			if(isset($_POST['caijiid']) and $_POST['caijiid'] != null ){
				
					$cjinfo['caijiid']=$_POST['caijiid'];
					$cjinfo['fullflag']=1;
			
			}else{
				$this->error('小说ID不能为空！');
			}
			
			if(isset($_POST['caijiname']) and $_POST['caijiname'] != null ){
				$cjinfo['articlename']=$_POST['caijiname'];
			}else{
				$this->error('本条规则不能为空！');
			}
			
			$caiji=M('article_article');
			$isInsert=$caiji->add($cjinfo);
			if($isInsert){
				$this->success('添加规则成功！');
			}else{
				$this->error('添加规则失败！');
			}
		}
		
		
	}

?>